//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Reflection;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration;

namespace EntLibContrib.Resource.Configuration
{
	/// <summary>
	/// Represents a null configuration source that always returns null for a section.
	/// </summary>
	public class InternalResourceConfigurationSource : IConfigurationSource
	{
		#region Constants
		private const string internalResourceManager = "InternalResourceManager";
		private const string internalResourceProvider = "InternalResourceProvider";
		private const string resourceAssemblyName = "EntLibContrib.Resource.Store";
		private const string resourceBaseName = "EntLibContrib.Resource.Store.Properties.InternalResources";
		#endregion

		#region Fields
		private static DictionaryConfigurationSource sections = InitializeSections();
		#endregion

		#region Public Methods
		/// <summary>
		/// Returns fixed values for the section.
		/// </summary>
		/// <param name="sectionName">The section name to retrieve.</param>
		/// <returns>Configuration section settings data.</returns>
		public ConfigurationSection GetSection(string sectionName)
		{
			if (sections.Contains(sectionName))
				return sections.GetSection(sectionName);
			else
				return null;
		}

		/// <summary>
		/// Null implementation of <see cref="IConfigurationSource.Add(IConfigurationParameter, string, ConfigurationSection)"/> that 
		/// ignores the request.
		/// </summary>
		/// <param name="saveParameter">The <see cref="IConfigurationParameter"/> that represents the location where 
		/// to save the updated configuration.</param>
		/// <param name="sectionName">The name by which the <paramref name="configurationSection"/> should be added.</param>
		/// <param name="configurationSection">The configuration section to add.</param>
		public void Add(IConfigurationParameter saveParameter, string sectionName, ConfigurationSection configurationSection)
		{
			sections.Add(saveParameter, sectionName, configurationSection);
		}

		/// <summary>
		/// Null implementation of <see cref="IConfigurationSource.Remove(IConfigurationParameter, string)"/> that 
		/// ignores the request.
		/// </summary>
		/// <param name="removeParameter">The <see cref="IConfigurationParameter"/> that represents the location where 
		/// to save the updated configuration.</param>
		/// <param name="sectionName">The name of the section to remove.</param>
		public void Remove(IConfigurationParameter removeParameter, string sectionName)
		{
			sections.Remove(removeParameter, sectionName);
		}

		/// <summary>
		/// Adds a handler to be called when changes to section <code>sectionName</code> are detected.
		/// </summary>
		/// <param name="sectionName">The name of the section to watch for.</param>
		/// <param name="handler">The handler.</param>
		public void AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
		{
			sections.AddSectionChangeHandler(sectionName, handler);
		}

		/// <summary>
		/// Remove a handler to be called when changes to section <code>sectionName</code> are detected.
		/// </summary>
		/// <param name="sectionName">The name of the section to watch for.</param>
		/// <param name="handler">The handler.</param>
		public void RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
		{
			sections.RemoveSectionChangeHandler(sectionName, handler);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Initializes the configuration sections.
		/// </summary>
		/// <returns></returns>
		private static DictionaryConfigurationSource InitializeSections()
		{
			DictionaryConfigurationSource configurationSections = new DictionaryConfigurationSource();
			
			ResourceManagerSettings resourceManagerSection = new ResourceManagerSettings();
			resourceManagerSection.DefaultResourceManager = internalResourceManager;
			resourceManagerSection.ResourceManagers.Add(new ResourceManagerData(internalResourceManager, internalResourceProvider));
			resourceManagerSection.ResourceProviders.Add(new InternalResourceProviderData(internalResourceProvider, resourceAssemblyName, resourceBaseName));
			configurationSections.Add(ResourceManagerSettings.SectionName, resourceManagerSection);

			InstrumentationConfigurationSection instrumentationConfigurationSection = new InstrumentationConfigurationSection(false, false, false);
			configurationSections.Add(InstrumentationConfigurationSection.SectionName, instrumentationConfigurationSection);
			
			return configurationSections;
		}
		#endregion
	}
}
